package org.erepublik.company.charts;

import java.util.ArrayList;
import java.util.Arrays;

import org.erepublik.company.EAlliance;
import org.erepublik.company.EWorld;
import org.erepublik.company.Location;
import org.erepublik.company.product.Food;
import org.erepublik.company.searchCriteria.EFoodSearchCriteria;
import org.erepublik.company.service.IVatService;
import org.erepublik.company.service.JSONFoodService;
import org.erepublik.company.service.XmlVatService;
import org.json.JSONArray;
import org.json.JSONException;

public class FoodJSONDataCollector extends AbstractJSONDataCollector<Food> {


    public FoodJSONDataCollector(IVatService vatService) {
        super("food", EFoodSearchCriteria.class, vatService);
    }

    @Override
    protected ArrayList<Food> getProductFromJSONArray(JSONArray jsonArrayWithFood) throws JSONException {
        ArrayList<Food> food = JSONFoodService.getFoodFromJSONArray(jsonArrayWithFood);
        return food;
    }

 
    final double SUM_ATTRIBUTE_LIMIT = 2000;
    @SuppressWarnings("unchecked")
    protected Food[] getNBestOffers(int offers, ArrayList<Food> food, Enum criteria, Location country) {
        Food[] foodArray = filter(food, country).toArray(new Food[0]);
//        System.out.println("getting N offers from "+criteria + " "+country);
        EFoodSearchCriteria comparator = (EFoodSearchCriteria)criteria;
        Arrays.sort(foodArray, comparator.getComparator());
        Food[] shorArray = new Food[Math.min(offers, foodArray.length)];
        for (int i=0; i < shorArray.length; i++) {
            shorArray[i] = foodArray[i];
        }
         
        return shorArray;
    }

    private ArrayList<Food> filter(ArrayList<Food> food, Location country) {
        if (country == EWorld.GLOBAL)
            return food;
        
        if (country instanceof EAlliance) {
            ArrayList<Food> filteredData = new ArrayList<Food>();
            for (Food food2 : food) {
                if (food2.getCountry().getAlliance()==country)
                    filteredData.add(food2);
            }
            return filteredData;
        }
        
        ArrayList<Food> filteredData = new ArrayList<Food>();
        for (Food food2 : food) {
            if (food2.getCountry()==country)
                filteredData.add(food2);
        }
        return filteredData;
    }

    // obliczanie sredniej wazonej (aga jest ilosc odyzkiwanego
    // wellness/happiness)
    @SuppressWarnings("unchecked")
    protected double calculateOffers(Food[] bestOffers, Enum criteria) {

        double attributeSum = 0.0d;
        double valueSum = 0.0d;
        for (Food food : bestOffers) {
            double attribute = 0.0d;
            attribute = food.getWellness();
            valueSum += food.getPriceInGold() * food.getQuantity();
            attributeSum += attribute * food.getQuantity();
            if (attributeSum>SUM_ATTRIBUTE_LIMIT)
                break;
        }
        if (valueSum==0)
            return 0.0d;
 
        
        return attributeSum / valueSum;
    }

    public static void main(String[] args) {
        FoodJSONDataCollector dataCollector = new FoodJSONDataCollector(new XmlVatService());
        dataCollector.updateData();
//        ChartDataSet dataset = dataCollector.getData(null, EFoodSearchCriteria.WELLNESS.toString());
//        List<ChartData> data = dataset.getData();

//        System.out.println( dataset.getGoogleChartFor7days() );
//        System.out.println( dataset.getGoogleChartFor24hours() );
//        System.out.println( dataset.getGoogleChartFor2months() );
    }

}
